package com.atguigu.gmall.order.api;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-03-10 10:45
 */
@RestController
@RequestMapping("/api/order")
public class OrderApiController {


    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 渲染用户订单确认页面相关参数
     *
     * @param request
     * @return
     */
    @GetMapping("/auth/trade")
    public Result<Map> tradeDatas(HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        //调用业务层封装相关数据
        Map<String, Object> mapData = orderInfoService.tradeDatas(userId);
        return Result.ok(mapData);
    }

    /**
     * 根据订单ID查询订单信息
     *
     * @param orderId
     * @return
     */
    @GetMapping("/inner/getOrderInfo/{orderId}")
    public OrderInfo getOrderInfo(@PathVariable("orderId") Long orderId) {
        return orderInfoService.getById(orderId);
    }


    /**
     * 根据订单编号查询订单信息
     * @param outTradeNo
     * @return
     */
    @GetMapping("/inner/getOrderInfoByOutTradeNo/{outTradeNo}")
    public OrderInfo getOrderInfoByOutradeNo(@PathVariable("outTradeNo") String outTradeNo) {
        return orderInfoService.getOrderInfoByOutradeNo(outTradeNo);
    }

    /**
     * 提供给秒杀服务调用RestFul接口实现:保存秒杀订单
     * @param orderInfo
     * @return 保存后秒杀订单ID
     */
    @PostMapping("/inner/seckill/submitOrder")
    public Long submitSeckillOrder(@RequestBody OrderInfo orderInfo){
        return orderInfoService.submitSeckillOrder(orderInfo);
    }
}
